package com.xiaoma.tpo.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoma.tpo.dao.ForumTopicDao;
import com.xiaoma.tpo.domain.ForumTopic;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.dto.ForumTeacherTopicVo;
import com.xiaoma.tpo.dto.ForumTopicVo;
import com.xiaoma.tpo.service.ForumService;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.tpo.utils.UploadImgUtil;

@Controller
@RequestMapping("/sys/forum")
public class ForumController {

    @Autowired
    private ForumTopicDao forumDao;
    
    @Autowired
    private ForumService forumService;
    
    @RequestMapping(value= "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(Integer page, Integer rows) {
        Map<String, Object> result = new HashMap<String, Object>();
        int startIndex = (page - 1) * rows;
        Pager pager = forumDao.findTeacherTopics(startIndex, rows);
        result.put("rows", pager.getList());
        result.put("total", pager.getTotalCount());
        return result;
    }
    
    @RequestMapping(value= "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveTopic(HttpSession session, @RequestParam MultipartFile imgs, String title, String content, Integer type, Integer teacherId){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            User user = (User)session.getAttribute("CURRENT_USER");
            if (user != null) {
                ForumTopic topic = new ForumTopic();
                if(imgs != null && !imgs.isEmpty()){
                    String path = UploadImgUtil.uploadImg(imgs.getBytes(), imgs.getOriginalFilename());
                    topic.setImgs(path);
                }
                Date now = new Date();
                topic.setContent(content);
                topic.setTitle(title);
                topic.setType(type);
                topic.setCreatedAt(now);
                topic.setUpdatedAt(now);
                topic.setTitle(title);
                if (teacherId != null) {
                    topic.setUserId(teacherId);
                } else {
                    topic.setUserId(user.getId());
                }
                forumService.saveForumTopic(topic);
                result.put("status", 1);
            } else {
                result.put("status", 0);
                result.put("message", "session invalid");
            }
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e);
        }
        return result;
    }
    
    @RequestMapping(value= "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> editTopic(HttpSession session, @RequestParam MultipartFile imgs, String title, String content, int id, Integer type,Integer teacherId){
        Map<String,Object> result = new HashMap<String, Object>();
        try {
            User user = (User)session.getAttribute("CURRENT_USER");
            if (user != null) {
                ForumTopic topic = forumService.getTopicById(id);
                if (topic != null) {
                    if(imgs != null && !imgs.isEmpty()){
                        String path = UploadImgUtil.uploadImg(imgs.getBytes(), imgs.getOriginalFilename());
                        topic.setImgs(path);
                    }
                    topic.setContent(content);
                    topic.setTitle(title);
                    topic.setType(type);
                    if (teacherId != null) {
                        topic.setUserId(teacherId);
                    }
                    forumService.updateForumTopic(topic);
                    result.put("status", 1);
                } else {
                    result.put("status", 0);
                    result.put("message", "topic can't find");
                }
            } else {
                result.put("status", 0);
                result.put("message", "session invalid");
            }
        } catch(Exception e) {
            result.put("status", 0);
            result.put("message", e);
        }
        return result;
    }
    
    @RequestMapping(value= "/remove/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> removeTopic(@PathVariable int id){
        Map<String,Object> result = new HashMap<String, Object>();
        forumService.deleteTopic(id);
        result.put("status", 1);
        return result;
    }
    
}
